<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/8
 * Time: 21:39
 */

namespace App\Services\Dao;

use App\Model\Session;

class SessionDao
{
    // 新增session数据
    public static function saveDeviceInfoToSessionTable($device_id,$user_uuid,$access_token,$refresh_token) {
        $session = new Session();

        $session->device_id = $device_id;
        $session->user_uuid = $user_uuid;
        $session->expire = config('session.lifetime');
        $session->access_token = $access_token;
        $session->refresh_token = $refresh_token;

        $session->save();
        return $session->toArray();
    }

    // 查询UUID
    public static function getSessionByDeviceIdAndUserUUID($device_id,$user_uuid) {
        $session = Session::where('device_id','=',$device_id)
                 ->where('user_uuid','=',$user_uuid)
                 ->first();

        if($session) {
            return $session->toArray();
        }

        return [];
    }

    public static function updateSession($device_id,$user_uuid,$data=[]) {
        $ret = Session::where('device_id','=',$device_id)
             ->where('user_uuid','=',$user_uuid)->update($data);

        return $ret;
    }
}